package aad.storyboard.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;

import utils.ViewUtils;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.util.Log;
import android.widget.ImageView;
/**
 *  svg type image, encodes how to construct a bitmap from a resource, and registers the endings for the factory pattern (see documentation)
 */
public class SVGImage extends Image {
	
		private RectF crop;
		private Picture picture;
        /**
         * registers the endings for the image factory
         */
		static{
			ImageFactory.getInstance().registerClass(".svg",new SVGImage());
		}
        public SVGImage(Resources res, int ID) throws FileNotFoundException {
                super(res, ID);
                // TODO Auto-generated constructor stub
        }
        
        private SVGImage(){
        	super();
        }
        /**
         * returns a bitmap from a svg file
         */

        @Override
        public Bitmap constructBitmap(Resources res, int iD) {
                SVG svg = SVGParser.getSVGFromResource(res, iD);
                picture = svg.getPicture();
        		RectF bounds = svg.getBounds();
        		RectF limits = svg.getLimits();
        		 crop = null;
        		//svgHeight = picture.getHeight();
        		//svgWidth = picture.getWidth();
        		if(limits != null){
        			crop = limits;
    				Log.e("TAG", "limits "+ limits.toString());
        			if(bounds != null){
        				crop = bounds;
        				Log.e("TAG", "bounds " + bounds.toString());
        			}
        		}
        		
        		boolean toCrop = true;
        		if(crop == null){
        			toCrop = false;
        			crop = new RectF();
        			crop.set(0, 0, picture.getWidth(), picture.getHeight());
        		}
        		float scaleFactor = ViewUtils.screenHeight/crop.height();
        		//Bitmap bMap = Bitmap.createBitmap(Math.round(scaleFactor*crop.width()),Math.round(scaleFactor*crop.height()), Bitmap.Config.ARGB_8888);
        		Bitmap bMap = Bitmap.createBitmap((int)Math.round(picture.getWidth()), (int)Math.round(picture.getHeight()), Bitmap.Config.ARGB_8888);
        		Log.e("TAG", "Created a bitmap of width:"+bMap.getWidth()+" height: "+bMap.getHeight());
        		Canvas c = new Canvas(bMap);
        		Log.e("TAG", "Scaling factor:"+scaleFactor);
        		Log.e("TAG", "Now have a bitmap of width:"+bMap.getWidth()+" height: "+bMap.getHeight());
        		c.drawPicture(picture);
        		bMap = Bitmap.createBitmap(bMap,(int)Math.floor(crop.left),(int)Math.floor(crop.top),(int)Math.ceil(bMap.getWidth()-crop.left),(int)Math.ceil(bMap.getHeight()-crop.top));
        		this.width = picture.getWidth();
        		this.height = picture.getHeight();
        		return bMap;
        }

        @Override
        protected Drawable constructDrawable(Resources res, int iD) {
                
                return null;
        }

        @Override
        public Drawable getDrawable() {
                // TODO Auto-generated method stub
                return null;
        }
        @Override
        public Bitmap getBitmap(){
        	if(this.bitmapChanged){
        		this.importScaledBitmap();
        		this.bitmapChanged = false;
        	}
        	return this.bitmap;
        }

        //helper method for importing a bitmap from svg
		private void importScaledBitmap() {
			float scaleFactorX = this.width/picture.getWidth();
			float scaleFactorY = this.height/picture.getHeight();
			Bitmap bMap = Bitmap.createBitmap(Math.round(this.width), Math.round(this.height), Bitmap.Config.ARGB_8888);
    		Canvas c = new Canvas(bMap);
    		c.scale(scaleFactorX,scaleFactorY);
    		c.drawPicture(picture);
    		crop.left = crop.left * scaleFactorX;
    		crop.right = crop.right * scaleFactorX;
    		crop.top = crop.top * scaleFactorY;
    		crop.bottom = crop.bottom * scaleFactorY;
    		
    		this.bitmap = Bitmap.createBitmap(bMap,(int)Math.floor(crop.left),(int)Math.floor(crop.top),(int)Math.ceil(bMap.getWidth()-crop.left),(int)Math.ceil(bMap.getHeight()-crop.top));		
    		
		}


        /**
         * returns a new image for the Image Factory (better explained in the docu
         */
		@Override
		public Image createImage(Resources r, int id) throws FileNotFoundException{
			return new SVGImage(r,id);
		}


}
